package com.itheima.service.mongo.impl;

import com.itheima.domain.db.Visitor;
import com.itheima.service.mongo.VisitorService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author zsk
 * @create 2022-03-20-17:48
 */
@DubboService
public class VisitorServiceImpl implements VisitorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询访客信息
    @Override
    public List<Visitor> findVisitors(Long userId, Long lastAccessTime) {
        //构建查询条件
        Query query = new Query(
                Criteria.where("userId").is(userId)
                .and("date").gt(lastAccessTime))//时间等于传递过来的时间
                .with(Sort.by(Sort.Order.desc("date")))//按照时间降序
                .skip(0).limit(5);//只查询前五条

        //查询并返回
        return mongoTemplate.find(query, Visitor.class);

    }
}
